
package la.foton.atividade.controller;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import la.foton.atividade.service.AuthenticationService;

@ManagedBean(name = "loginBean")
@RequestScoped
public class LoginBean
{

   @ManagedProperty(value = "#{authenticationService}")
   private AuthenticationService authenticationService;

   private String userName;
   private String password;

   public String login()
   {
      boolean success = authenticationService.login(userName, password);

      if (!success)
      {
         FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Login ou senha inválidos");
         FacesContext.getCurrentInstance().addMessage(null, facesMessage);
         return "falhaLogin";
      }

      return "sucessoLogin";
   }

   public String logout()
   {
      authenticationService.logout();
      return "login";
   }

   public String getUsuarioLogado()
   {
      return authenticationService.getUsuarioLogado().getUsername();
   }

   public String getUserName()
   {
      return userName;
   }

   public void setUserName(String userName)
   {
      this.userName = userName;
   }

   public String getPassword()
   {
      return password;
   }

   public void setPassword(String password)
   {
      this.password = password;
   }

   public void setAuthenticationService(AuthenticationService authenticationService)
   {
      this.authenticationService = authenticationService;
   }

}
